//
//  TextFieldDemo.m
//  Exercise
//
//  Created by zhouxiaobo on 15/11/12.
//  Copyright © 2015年 mapath. All rights reserved.
//

#import "TextFieldDemo.h"

@interface TextFieldDemo ()

@end

@implementation TextFieldDemo

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(screenWidth/2-60, 80, 120, 30);
    btn.backgroundColor = [UIColor greenColor];
    [btn setTitle:@"输入框失去焦点" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(lostFocus) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(screenWidth/2-60, 150, 120, 30)];
    tf.tag = 101;
    tf.borderStyle = UITextBorderStyleRoundedRect; //设置输入框的样式
    tf.clearButtonMode = UITextFieldViewModeWhileEditing; //设置后面的清空按钮出现
    tf.clearsOnBeginEditing = YES; //设置开始编辑，就清空文本框
    tf.adjustsFontSizeToFitWidth = YES; //会自动调节文字的大小
    //tf.secureTextEntry = YES; //设置成密码
    tf.placeholder = @"请输入用户信息";
    
    //设置代理
    tf.delegate = self;
    
    [self.view addSubview:tf];
    
    //自定义Clear按钮
    UIView *clearView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    clearView.backgroundColor = [UIColor yellowColor];
    tf.rightView = clearView;
    tf.rightViewMode = UITextFieldViewModeWhileEditing;

    //自定义弹出键盘
//    UIView *inputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 100)];
//    inputView.backgroundColor = [UIColor yellowColor];
//    tf.inputView = inputView;
    
    //自定义弹出键盘和系统键盘一起出来
    UIView *inputView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 40)];
    inputView2.backgroundColor = [UIColor yellowColor];
    tf.inputAccessoryView = inputView2;
    
    //键盘设置
    //tf.keyboardType = UIKeyboardTypeNumberPad; //纯数字
    tf.returnKeyType = UIReturnKeyDone; //设置返回为Done
    tf.autocapitalizationType = YES;    //开始小写
}

- (void)lostFocus {
    UITextField *tf = [self.view viewWithTag:101];
    [tf resignFirstResponder]; //移除键盘
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    NSLog(@"textFieldShouldBeginEditing:%@", textField.text);
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"textFieldDidBeginEditing:%@", textField.text);
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSLog(@"textFieldShouldEndEditing:%@", textField.text);
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"textFieldDidEndEditing:%@", textField.text);
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"shouldChangeCharactersInRange:%@", textField.text);
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    NSLog(@"textFieldShouldClear:%@", textField.text);
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog(@"textFieldShouldReturn:%@", textField.text);
    return YES;
}


@end
